package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.service.ISysGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSuppliergoodsimplower;
import com.ruoyi.system.service.ISysSuppliergoodsimplowerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商货物授权Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/suppliergoodsimplower")
public class SysSuppliergoodsimplowerController extends BaseController
{
    private String prefix = "system/suppliergoodsimplower";

    @Autowired
    private ISysSuppliergoodsimplowerService sysSuppliergoodsimplowerService;
    @Autowired
    private ISysGoodsService sysGoodsService;

    @RequiresPermissions("system:suppliergoodsimplower:view")
    @GetMapping()
    public String suppliergoodsimplower()
    {
        return prefix + "/suppliergoodsimplower";
    }

    /**
     * 查询供应商货物授权列表
     */
    @RequiresPermissions("system:suppliergoodsimplower:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSuppliergoodsimplower sysSuppliergoodsimplower)
    {
        startPage();
        List<SysSuppliergoodsimplower> list = sysSuppliergoodsimplowerService.selectSysSuppliergoodsimplowerList(sysSuppliergoodsimplower);
        return getDataTable(list);
    }

    /**
     * 导出供应商货物授权列表
     */
    @RequiresPermissions("system:suppliergoodsimplower:export")
    @Log(title = "供应商货物授权", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSuppliergoodsimplower sysSuppliergoodsimplower)
    {
        List<SysSuppliergoodsimplower> list = sysSuppliergoodsimplowerService.selectSysSuppliergoodsimplowerList(sysSuppliergoodsimplower);
        ExcelUtil<SysSuppliergoodsimplower> util = new ExcelUtil<SysSuppliergoodsimplower>(SysSuppliergoodsimplower.class);
        return util.exportExcel(list, "suppliergoodsimplower");
    }

    /**
     * 新增供应商货物授权
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存供应商货物授权
     */
    @RequiresPermissions("system:suppliergoodsimplower:add")
    @Log(title = "供应商货物授权", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSuppliergoodsimplower sysSuppliergoodsimplower)
    {
        return toAjax(sysSuppliergoodsimplowerService.insertSysSuppliergoodsimplower(sysSuppliergoodsimplower));
    }
    /**
     * 新增保存供应商货物授权
     */
    @Log(title = "供应商货物授权", businessType = BusinessType.INSERT)
    @PostMapping("/addpatch")
    @ResponseBody
    public AjaxResult addpatch(Long id,String[] grantGoods)
    {
        int result=0;
        for(String goodsId:grantGoods){
            SysGoods goods=sysGoodsService.selectSysGoodsById(Long.parseLong(goodsId));
            SysSuppliergoodsimplower sysSuppliergoodsimplower=new SysSuppliergoodsimplower();
            sysSuppliergoodsimplower.setGoodsId(goods.getGoodsId());
            sysSuppliergoodsimplower.setSupplierId(id);
            result+=sysSuppliergoodsimplowerService.insertSysSuppliergoodsimplower(sysSuppliergoodsimplower);
        }
        System.out.println(result+"======"+grantGoods.length);
        if(result==grantGoods.length){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }

    }
    /**
     * 修改供应商货物授权
     */
    @GetMapping("/edit/{supplierGoodsImplowerId}")
    public String edit(@PathVariable("supplierGoodsImplowerId") Long supplierGoodsImplowerId, ModelMap mmap)
    {
        SysSuppliergoodsimplower sysSuppliergoodsimplower = sysSuppliergoodsimplowerService.selectSysSuppliergoodsimplowerById(supplierGoodsImplowerId);
        mmap.put("sysSuppliergoodsimplower", sysSuppliergoodsimplower);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商货物授权
     */
    @RequiresPermissions("system:suppliergoodsimplower:edit")
    @Log(title = "供应商货物授权", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSuppliergoodsimplower sysSuppliergoodsimplower)
    {
        return toAjax(sysSuppliergoodsimplowerService.updateSysSuppliergoodsimplower(sysSuppliergoodsimplower));
    }

    /**
     * 删除供应商货物授权
     */
    @RequiresPermissions("system:suppliergoodsimplower:remove")
    @Log(title = "供应商货物授权", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysSuppliergoodsimplowerService.deleteSysSuppliergoodsimplowerByIds(ids));
    }
}
